﻿<UserControl x:Class="ObjectEditor"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:UI="clr-namespace:RawExplorer2.UI"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:RawExplorer2.UI"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             HorizontalAlignment="Stretch"
             VerticalAlignment="Stretch"
             mc:Ignorable="d">
	<UserControl.Resources>
		<ResourceDictionary>
			<UI:ArgumentTypeTemplateSelector x:Key="ArgumentTypeTemplateSelector" />
			<SolidColorBrush x:Key="GridLineBrush" Color="LightGray" />
			<ResourceDictionary.MergedDictionaries>
				<ResourceDictionary Source="ArgTemplates\BitArgDataTemplate.xaml" />
				<ResourceDictionary Source="ArgTemplates\ColorNumberArgDataTemplate.xaml" />
				<ResourceDictionary Source="ArgTemplates\StringArgDataTemplate.xaml" />
				<ResourceDictionary Source="ArgTemplates\ListArgDataTemplate.xaml" />
				<ResourceDictionary Source="ArgTemplates\CharCodeArgDataTemplate.xaml" />
				<ResourceDictionary Source="ArgTemplates\MaterialArgDataTemplate.xaml" />
			</ResourceDictionary.MergedDictionaries>
		</ResourceDictionary>
	</UserControl.Resources>

	<Grid>
		<Grid.RowDefinitions>
			<RowDefinition Height="3*" MinHeight="200" />
			<RowDefinition Height="5" />
			<RowDefinition Height="*" MinHeight="100" />
		</Grid.RowDefinitions>
		<Border BorderBrush="Gray"
		        BorderThickness="1"
		        SnapsToDevicePixels="True">
			<DockPanel Grid.Row="0" VerticalAlignment="Stretch">
				<ToolBarPanel Background="{x:Static SystemColors.ControlLightBrush}" DockPanel.Dock="Top">
					<ToolBar Margin="-3,0,-1,0" ToolBarTray.IsLocked="True">
						<Button Command="{Binding Path=InsertTokenCommand}" IsEnabled="{Binding Path=IsObjectLoaded}">
							<StackPanel Orientation="Horizontal">
								<local:AutoGreyableImage Width="16"
								                         Height="16"
								                         Source="/RawExplorer2;component/Icons/Symbol Blue Add.png"
								                         Stretch="Fill" />
								<TextBlock Padding="2,0,0,0">Insert</TextBlock>
							</StackPanel>
						</Button>
						<Button Command="{Binding Path=DeleteSelectedTokensCommand}" IsEnabled="{Binding Path=IsObjectLoaded}">
							<StackPanel Orientation="Horizontal">
								<local:AutoGreyableImage Width="16"
								                         Height="16"
								                         Source="/RawExplorer2;component/Icons/Symbol Blue Restrict.png"
								                         Stretch="Fill" />
								<TextBlock Padding="2,0,0,0">Delete</TextBlock>
							</StackPanel>
						</Button>
						<Separator />
						<Button Command="{Binding Path=MoveTokenUpCommand}" IsEnabled="{Binding Path=IsObjectLoaded}">
							<StackPanel Orientation="Horizontal">
								<local:AutoGreyableImage Width="16"
								                         Height="16"
								                         Source="/RawExplorer2;component/Icons/Navigation Blue Up.png"
								                         Stretch="Fill" />
								<TextBlock Padding="2,0,0,0">Move Up</TextBlock>
							</StackPanel>
						</Button>
						<Button Command="{Binding Path=MoveTokenDownCommand}" IsEnabled="{Binding Path=IsObjectLoaded}">
							<StackPanel Orientation="Horizontal">
								<local:AutoGreyableImage Width="16"
								                         Height="16"
								                         Source="/RawExplorer2;component/Icons/Navigation Blue Down.png"
								                         Stretch="Fill" />
								<TextBlock Padding="2,0,0,0">Move Down</TextBlock>
							</StackPanel>
						</Button>
					</ToolBar>
				</ToolBarPanel>
				<DataGrid x:Name="grdTokens"
				          VerticalAlignment="Stretch"
				          AlternationCount="2"
				          AutoGenerateColumns="False"
				          BorderThickness="0"
				          CanUserDeleteRows="False"
				          CanUserReorderColumns="False"
				          CanUserResizeColumns="False"
				          CanUserResizeRows="False"
				          CanUserSortColumns="False"
				          HorizontalGridLinesBrush="{StaticResource GridLineBrush}"
				          ItemsSource="{Binding Path=CurrentObjectVM.Tokens}"
				          RowDetailsVisibilityMode="Collapsed"
				          RowHeaderWidth="0"
				          ScrollViewer.CanContentScroll="True"
				          SelectedItem="{Binding Path=SelectedToken,
				                                 Mode=TwoWay}"
				          SelectionMode="Extended"
				          VerticalGridLinesBrush="{StaticResource GridLineBrush}"
				          VirtualizingPanel.IsVirtualizing="True"
				          VirtualizingPanel.ScrollUnit="Pixel">
					<DataGrid.Resources>
						<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="{x:Static SystemColors.HighlightColor}" />
						<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="{x:Static SystemColors.HighlightTextColor}" />
						<Style TargetType="{x:Type DataGridRow}">
							<Style.Triggers>
								<Trigger Property="AlternationIndex" Value="1">
									<!--  <Setter Property="Background" Value="#FDFDFF" />  -->
								</Trigger>
							</Style.Triggers>
						</Style>
					</DataGrid.Resources>
					<DataGrid.RowStyle>
						<Style TargetType="{x:Type DataGridRow}">
							<Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
						</Style>
					</DataGrid.RowStyle>
					<DataGrid.Columns>
						<DataGridTemplateColumn Width="Auto" Header="Token">
							<DataGridTemplateColumn.CellTemplate>
								<DataTemplate>
									<StackPanel HorizontalAlignment="Stretch" Orientation="Horizontal">
										<Border Name="RowDetailExpander"
										        Background="Transparent"
										        MouseDown="RowDetailExpander_MouseDown">
											<Path Width="9"
											      Margin="4"
											      VerticalAlignment="Center"
											      Stretch="Uniform"
											      Stroke="Black">
												<Path.Style>
													<Style TargetType="Path">
														<Setter Property="Data" Value="{StaticResource PlusBox}" />
														<Setter Property="Visibility" Value="Hidden" />
														<Style.Triggers>
															<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridRow}, Path=DetailsVisibility}" Value="Visible">
																<Setter Property="Data" Value="{StaticResource MinusBox}" />
															</DataTrigger>
															<DataTrigger Binding="{Binding CanShowDetails}" Value="True">
																<Setter Property="Visibility" Value="Visible" />
															</DataTrigger>
														</Style.Triggers>
													</Style>
												</Path.Style>
											</Path>
										</Border>
										<TextBlock VerticalAlignment="Center" Text="{Binding Name}" />
									</StackPanel>
								</DataTemplate>
							</DataGridTemplateColumn.CellTemplate>
						</DataGridTemplateColumn>
						<DataGridTemplateColumn Width="*" Header="Arguments">
							<DataGridTemplateColumn.CellTemplate>
								<DataTemplate>
									<TextBlock Text="{Binding Path=ArgumentsAsString, Mode=Oneway}" TextWrapping="Wrap" />
								</DataTemplate>
							</DataGridTemplateColumn.CellTemplate>
						</DataGridTemplateColumn>
					</DataGrid.Columns>
					<DataGrid.RowDetailsTemplate>
						<DataTemplate>
							<DataGrid Name="grdArguments"
							          Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
							                                                         AncestorType={x:Type ScrollContentPresenter}},
							                          Path=ActualWidth,
							                          Converter={UI:NestedGridWidthHackConverter}}"
							          Margin="20,0,0,4"
							          HorizontalAlignment="Left"
							          AlternationCount="2"
							          AutoGenerateColumns="False"
							          CanUserAddRows="{Binding Path=CanAddArguments}"
							          CanUserDeleteRows="False"
							          CanUserReorderColumns="False"
							          CanUserResizeColumns="False"
							          CanUserResizeRows="False"
							          CanUserSortColumns="False"
							          HorizontalGridLinesBrush="{StaticResource GridLineBrush}"
							          ItemsSource="{Binding Path=Arguments}"
							          RowHeaderWidth="0"
							          ScrollViewer.CanContentScroll="False"
							          VerticalGridLinesBrush="{StaticResource GridLineBrush}">

								<!--  Override the datagrid to remove the scrollview so that is doesnt eat the scroll event.  The child datagrid doesn't need to scroll anyway  -->
								<DataGrid.Template>
									<ControlTemplate TargetType="{x:Type DataGrid}">
										<Border Background="{TemplateBinding Background}"
										        BorderBrush="{TemplateBinding BorderBrush}"
										        BorderThickness="{TemplateBinding BorderThickness}"
										        Padding="{TemplateBinding Padding}"
										        SnapsToDevicePixels="True">
											<StackPanel>
												<DataGridColumnHeadersPresenter Name="PART_ColumnHeadersPresenter"
												                                Grid.Column="0"
												                                Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
												                                                     Path=HeadersVisibility,
												                                                     Converter={x:Static DataGrid.HeadersVisibilityConverter},
												                                                     ConverterParameter={x:Static DataGridHeadersVisibility.Column}}" />
												<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
											</StackPanel>
										</Border>
									</ControlTemplate>
								</DataGrid.Template>
								<!--  End datagrid scroll  -->

								<DataGrid.Resources>
									<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="{x:Static SystemColors.HighlightColor}" />
									<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="{x:Static SystemColors.HighlightTextColor}" />
								</DataGrid.Resources>
								<DataGrid.Columns>
									<DataGridTemplateColumn Width="Auto" Header="Name">
										<DataGridTemplateColumn.CellTemplate>
											<DataTemplate>
												<TextBlock VerticalAlignment="Center"
												           Padding="3"
												           Text="{Binding Path=Name}" />
											</DataTemplate>
										</DataGridTemplateColumn.CellTemplate>
									</DataGridTemplateColumn>
									<DataGridTemplateColumn Width="Auto" Header="Type">
										<DataGridTemplateColumn.CellTemplate>
											<DataTemplate>
												<TextBlock VerticalAlignment="Center"
												           Padding="3"
												           Text="{Binding Path=Type}" />
											</DataTemplate>
										</DataGridTemplateColumn.CellTemplate>
									</DataGridTemplateColumn>
									<DataGridTemplateColumn Width="*"
									                        CellTemplateSelector="{StaticResource ArgumentTypeTemplateSelector}"
									                        Header="Value" />
								</DataGrid.Columns>
							</DataGrid>
						</DataTemplate>
					</DataGrid.RowDetailsTemplate>
				</DataGrid>
			</DockPanel>
		</Border>
		<GridSplitter Grid.Row="1"
		              Height="5"
		              HorizontalAlignment="Stretch"
		              Background="Transparent" />
		<UI:DefinitionViewer Grid.Row="2" DataContext="{Binding Path=Definition}" />
	</Grid>

</UserControl>
